import axios from "axios";
import { Toast } from "vant";
// 设置默认的请求超时时间
// 设置默认请求前缀
// 设置默认content-type格式
// 创建 axios 实例
const instance = axios.create({
	// baseURL: '',
	timeout: 30000,
	headers: {
		"Content-Type": "application/json",
	},
});

// 请求拦截器
axios.interceptors.request.use(config => {
	const token = window.localStorage.getItem("token");
	const headers = {};
	token ? (headers.authorization = token) : "";
	return {
		...config,
		headers,
	};
});
// 响应拦截器
axios.interceptors.response.use(
	response => {
		return response.data;
	},
	err => {
		switch (err.response.status) {
			case 401:
				Toast("没有权限");
				break;
			case 403:
				Toast("身份过期");
				break;
			case 404:
				Toast("接口不存在");
				break;
			default:
				Toast("服务器错误");
				break;
		}
		return Promise.reject(err);
	}
);

export default axios;
